/*
 * TCSS 360 - Winter 2014
 * TESK Software
 */
package view.Papers;

import java.util.ArrayList;

import javax.swing.JPanel;

import view.MainFrame;
import view.userProfile.SubProgramChairView;

import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JLabel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FlowLayout;
import java.awt.Color;
import javax.swing.SwingConstants;
import javax.swing.DefaultComboBoxModel;
import javax.swing.AbstractListModel;
import javax.swing.ListSelectionModel;


/**
 * Allows a sub program chair to assign reviewers to papers.
 * @author Emily, Kon
 * @version March 19, 2014
 */
@SuppressWarnings("serial")
public class AssignReviewerstoPapersView extends JPanel {

	/** The frame the panel is associated with. */
	private MainFrame my_frame;
	
	/** The combo box that contains available reviewers. */
	private JComboBox<String> reviewerComboBox;
	
	/** A string reprsenting the current conference being used. */
	private String current_conference;

	/**
	 * Constructs the assign reviewers to papers view. 
	 * 
	 * @param the_mainframe the frame the panel should be associated with
	 * @param the_conference the conference that is currently being used
	 */
	@SuppressWarnings("unchecked")
	public AssignReviewerstoPapersView( MainFrame the_mainframe, String the_conference) {
		super();
		setBackground(new Color(255, 255, 255));
		my_frame = the_mainframe;
		current_conference = the_conference;
		setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		final JPanel displayPanel = new JPanel();
		displayPanel.setPreferredSize(new Dimension(500, 400));
		displayPanel.setBackground(new Color(240, 255, 240));
		add(displayPanel);
		displayPanel.setLayout(null);

		ArrayList<String> reviewerList = my_frame.db.getReviewers(current_conference);
		String[] reviewerArray = new String[reviewerList.size() + 1];
		reviewerArray[0] = null;
		for (int i = 0; i < reviewerList.size(); i++) {
			reviewerArray[i + 1] = reviewerList.get(i);
		}

		reviewerComboBox = new JComboBox<String>();
		reviewerComboBox.setModel(new DefaultComboBoxModel<String>(reviewerArray));
		reviewerComboBox.setFont(new Font("Tahoma", Font.PLAIN, 14));
		reviewerComboBox.setBounds(280, 140, 176, 31);
		displayPanel.add(reviewerComboBox);

		ArrayList<String> scPaperList = my_frame.db.getPapersAssignedForSC(my_frame.currentUser[0][0]);
		final String[] scPaperArray = new String[scPaperList.size()];
		for (int i = 0; i < scPaperList.size(); i++) {
			scPaperArray[i] = scPaperList.get(i);
		}

		final JList<String> papersList = new JList<String>();
		papersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		papersList.setModel(new AbstractListModel() {
			String[] values = scPaperArray;
			public int getSize() {
				return values.length;
			}
			public Object getElementAt(int index) {
				return values[index];
			}
		});

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(42, 102, 211, 240);
		displayPanel.add(scrollPane);
		scrollPane.setColumnHeaderView(papersList);
		scrollPane.setViewportView(papersList);

		JButton AssignButton = new JButton("Assign");
		AssignButton.setToolTipText("Click to assign a paper to a reviewer");
		AssignButton.setForeground(new Color(0, 0, 255));
		AssignButton.setBounds(299, 199, 119, 31);
		AssignButton.setFont(new Font("Tahoma", Font.BOLD, 14));
		AssignButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String reviewer = (String)reviewerComboBox.getSelectedItem();
				String paper = (String)papersList.getSelectedValue();
				if (reviewer == null && paper == null){
					JOptionPane.showMessageDialog(my_frame,  "Please select a paper and a reviewer to assign!", "Error!", JOptionPane.ERROR_MESSAGE);
				}
				else if (reviewer == null) {
					JOptionPane.showMessageDialog(my_frame,  "Please select a reviewer!", "Error!", JOptionPane.ERROR_MESSAGE);
				} else if (papersList.isSelectionEmpty() || paper == null) {
					JOptionPane.showMessageDialog(my_frame, "Please select a paper!", "Error!", JOptionPane.ERROR_MESSAGE);
				} else {
					String author = my_frame.db.getAuthor(paper, current_conference);
					ArrayList<String> reviewerPapers = my_frame.db.getPapersAssignedForReviewer(reviewer);
					int paperCount = 0;
					for (int i = 0; i < reviewerPapers.size(); i++) {
						if (reviewerPapers.get(i) != "") {
							paperCount++;
						}
					}
					if (paperCount >= 3) {
						JOptionPane.showMessageDialog(my_frame, "Reviewers already has 4 reviews assigned!", "Error!", JOptionPane.ERROR_MESSAGE);
					} else {
						my_frame.db.assignReviewer(reviewer, current_conference, author, paper);
						JOptionPane.showMessageDialog(my_frame, "Sucessfully Assigned Paper!", "Sucess!", JOptionPane.PLAIN_MESSAGE);
					}
				}
			}
		});
		displayPanel.add(AssignButton);

		JLabel lblAvailableReviewers = new JLabel("Available Reviewers");
		lblAvailableReviewers.setHorizontalAlignment(SwingConstants.CENTER);
		lblAvailableReviewers.setForeground(new Color(153, 50, 204));
		lblAvailableReviewers.setBounds(280, 97, 176, 31);
		displayPanel.add(lblAvailableReviewers);
		lblAvailableReviewers.setFont(new Font("Tahoma", Font.PLAIN, 16));

		JLabel lblPapers = new JLabel("Papers");
		lblPapers.setForeground(new Color(153, 50, 204));
		lblPapers.setBounds(109, 71, 73, 30);
		displayPanel.add(lblPapers);
		lblPapers.setFont(new Font("Urdu Typesetting", Font.BOLD, 16));

		JButton cancelButton = new JButton("Back");
		cancelButton.setForeground(new Color(0, 0, 255));
		cancelButton.setFont(new Font("Tahoma", Font.BOLD, 14));
		cancelButton.setBounds(299, 261, 119, 31);
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				my_frame.setNewPanel(new SubProgramChairView(my_frame));
			}
		});
		displayPanel.add(cancelButton);


	}
}
